#!/bin/sh
#
# Copyright (C) 2000-2025 Kern Sibbald
# License: BSD 2-Clause; see file LICENSE-FOSS
#
# Run a simple backup of the Bacula build directory then copy it
#   to another device.
#
# This script uses the virtual disk autochanger
#
# XPARAM SPOOLDATA_JOB=(default|yes|no)
# enable SpoolData into the Job definition or let the default (aka
# no SpoolData in the Job)
# XPARAM SPOOLDATA_RUN=(default|yes|no)
# enable spooldata=(yes|no) in the run command or let the default,
# don't specify the spooldata option in the run command
# XPARAM NEXT_NODEDUP=(default|yes|no)
# if dedup is enable and NEXT_NODEDUP=yes, then change the
# target device of the copy job to a normal device (not a dedup one)
# this force the copy job to rehydrate the data
#
# use it this way
# ( for run in default no yes ; do for job in default no yes ; do SPOOLDATA_RUN=$run \
# SPOOLDATA_JOB=$job REGRESS_DEBUG=1 tests/copy-job-test ; done ; done ) | \
# egrep "SETUP|JobId 9: Spooling data"
#
TestName="copy-job-test"
JobName=CopyJobSave
. scripts/functions


scripts/cleanup
scripts/copy-migration-confs
echo "${cwd}/build" >${cwd}/tmp/file-list
sed 's/migrate/copy/g' ${cwd}/bin/bacula-dir.conf > ${cwd}/tmp/1
sed 's/Migrate/Copy/g' ${cwd}/tmp/1 > ${cwd}/bin/bacula-dir.conf
change_jobname NightlySave $JobName

if true ; then
   # add a dummy client for the copy-job to test for bug #9116
   $bperl -e "add_attribute('$conf/bacula-dir.conf', 'Client', 'dummy-fd', 'Job', 'copy-job')"
   cat >> $conf/bacula-dir.conf <<EOF
Client {
  Name = dummy-fd
  Address = 8.8.8.8
  FDPort = 8102
  FdStorageAddress = 8.8.8.8
  Catalog = MyCatalog
  Password = "xevrjURYoCHhn26RaJoWbeWXEY/a3VqGKp/37tgWiuHc"          # password for FileDaemon
  File Retention = 30d                # 30 days
  Job Retention = 180d                # six months
  AutoPrune = yes                     # Prune expired Jobs/Files
  Maximum Concurrent Jobs = 4
}

EOF
fi

SPOOLDATA_JOB=${SPOOLDATA_JOB:-default}
if [ "$SPOOLDATA_JOB" = "yes" ] ; then
   $bperl -e "add_attribute('$conf/bacula-dir.conf', 'SpoolData', 'yes', 'Job', 'copy-job')"
elif [ "$SPOOLDATA_JOB" = "no" ] ; then
   $bperl -e "add_attribute('$conf/bacula-dir.conf', 'SpoolData', 'no', 'Job', 'copy-job')"
else
   /bin/true # don't set the SpoolData directive
fi

SPOOLDATA_RUN=${SPOOLDATA_RUN:-default}
if [ "$SPOOLDATA_RUN" = "yes" ] ; then
   run_spooldata="spooldata=yes"
elif [ "$SPOOLDATA_RUN" = "no" ] ; then
   run_spooldata="spooldata=no"
else
   run_spooldata=""
fi

start_test

NEXT_NODEDUP=${NEXT_NODEDUP:-default}
echo NEXT_NODEDUP=$NEXT_NODEDUP FORCE_DEDUP=$FORCE_DEDUP
if [ "$NEXT_NODEDUP" = "yes" -a "$FORCE_DEDUP" = "yes" ] ; then
   echo  DO IT
   $bperl -e "add_attribute('$conf/bacula-sd.conf', 'Device Type', 'File', 'Device', 'Drive-0')"
elif [ "$NEXT_NODEDUP" = "no" -a "$FORCE_DEDUP" = "yes" ] ; then
   /bin/true # don't change anything
elif [ "$FORCE_DEDUP" = "yes" ] ; then
   /bin/true # don't change anything
fi

echo === SETUP === SPOOLDATA_JOB=$SPOOLDATA_JOB SPOOLDATA_RUN=$SPOOLDATA_RUN NEXT_NODEDUP=$NEXT_NODEDUP

#
# Note, we first backup into Pool Default, 
#          then Copy into Pool Full. 
#              Pool Default uses Storage=File
#              Pool Full    uses Storage=DiskChanger

# Write out bconsole commands
cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@output /dev/null
messages
@$out ${cwd}/tmp/log1.out
@# setdebug level=100 storage=File
label storage=File volume=FileVolume001 Pool=Default
label storage=DiskChanger volume=ChangerVolume001 slot=1 Pool=Full drive=0
label storage=DiskChanger volume=ChangerVolume002 slot=2 Pool=Full drive=0
@# run two jobs (both will be copied)
run job=$JobName yes
run job=$JobName yes
run job=$JobName yes
wait
list jobs
list volumes
@#setdebug level=100 dir
@# should copy two jobs
@#setdebug level=51 storage=DiskChanger
@#setdebug level=100 storage=File tags=dedup,asx,network options=h
dedup rehydra_check_hash=1
run job=copy-job $run_spooldata comment="from control job" yes
wait
messages
@#purge volume=FileVolume001
list jobs 
list volumes
wait
messages
@$out ${cwd}/tmp/log3.out
@#
@# Now do another backup, but level Incremental
@#
run job=$JobName level=Incremental yes
wait
messages
@#
@# This final job that runs should be Incremental and
@# not upgraded to full.
list jobs
@# 
@# now do a restore
@#
@$out ${cwd}/tmp/log2.out
list volumes 
@#setdebug level=15 director
@#setdebug level=150 storage=DiskChanger
@# Note, here we are restoring from the original backup,
@#  so if you specify storage=DiskChanger the restore will fail
restore client=$CLIENT where=${cwd}/tmp/bacula-restores select
unmark *
mark *
done
yes
list volumes
wait
messages
@$out $tmp/log4.out
.jlist jobid=6
quit
END_OF_DATA

run_bacula
check_for_zombie_jobs storage=File client=${CLIENT}
stop_bacula

check_two_logs
check_restore_diff

grep 'Backup Level:' tmp/log3.out  | grep Incremental > /dev/null
if [ $? != 0 ]; then
    bstat=2
    print_debug "The incremental job must use copied jobs"
fi

end_test
